package org.hako.recite.word.test.impl.gui.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.hako.recite.word.Constants;
import org.hako.recite.word.test.impl.gui.NullWordTestBehavior;
import org.hako.recite.word.test.impl.gui.WordTestBehavior;

/**
 * Home shell. This is the first and main shell of application.
 * 
 * @since 0.4.3
 * @author <a href="mailto:xnnyygn@gmail.com">xnnyygn</a>
 */
public class HomeShell extends AbstractReciteWordShell {

	/**
	 * Create
	 */
	public HomeShell() {
		this(new NullWordTestBehavior());
	}

	/**
	 * Create
	 */
	public HomeShell(WordTestBehavior behavior) {
		super(SWT.DIALOG_TRIM, behavior);
		this.behavior = behavior;
		shell.setText(Constants.APPLICATION_NAME + ' ' + Constants.VERSION);
		createButtonsComposite(shell);
		shell.setSize(400, 300);
		center();
	}

	/**
	 * Create composite which contains buttons.
	 * 
	 * @param parentComposite
	 *            parent composite
	 */
	private void createButtonsComposite(Composite parentComposite) {
		parentComposite.setLayout(new GridLayout());

		// layout of composite in parent composite
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.CENTER;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		Composite composite = new Composite(parentComposite, SWT.NONE);
		composite.setLayoutData(gridData);
		FillLayout buttonsCompositeLayout = new FillLayout(SWT.VERTICAL);
		buttonsCompositeLayout.spacing = 5;
		composite.setLayout(buttonsCompositeLayout);
		addButtonsToComposite(composite);
	}

	/**
	 * Create buttons and add to parent composite.
	 * 
	 * @param composite
	 */
	private void addButtonsToComposite(Composite composite) {
		Button btnStart = createButton("Start Word Test", composite);
		btnStart.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent event) {
				behavior.showTestSettingWindow();
			}

		});
		Button btnExit = createButton("Exit", composite);
		btnExit.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent event) {
				shell.close();
			}

		});
		shell.setDefaultButton(btnStart);
	}

	/**
	 * Create button.
	 * 
	 * @param text
	 * @param parentComposite
	 * @return
	 */
	private Button createButton(String text, Composite parentComposite) {
		Button button = new Button(parentComposite, SWT.PUSH);
		button.setText(text);
		return button;
	}

}
